package com.elementum.olegomatic.persist.impl;

import com.elementum.olegomatic.dictionaries.DictId;
import com.elementum.olegomatic.dictionaries.Dictionary;
import com.elementum.olegomatic.dictionaries.DictionaryFactory;
import com.elementum.olegomatic.dictionaries.impl.DictionaryFactoryImpl;
import com.elementum.olegomatic.persist.BasicDAO;
import com.elementum.olegomatic.persist.Tweet;
import com.elementum.olegomatic.util.TweetUtil;
import com.mongodb.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: ravikolli
 * Date: 10/9/13
 * Time: 8:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class BasicDAOImpl implements BasicDAO{

    public Dictionary getSites(){

        Dictionary sites;
        DictionaryFactory dict = new DictionaryFactoryImpl();
        sites = dict.getDictionary(DictId.Sites);
        return sites;

    }
    public Dictionary getEvents(){

        Dictionary events;
        DictionaryFactory dict = new DictionaryFactoryImpl();
        events = dict.getDictionary(DictId.Events);
        return events;
    }

    public List<Tweet> getReceivedTweets(){

        TweetUtil util = new TweetUtil();
        List<Tweet> tweetList=new ArrayList<Tweet>();
        List<Tweet> tweetWithSites= new ArrayList<Tweet>();
        DBCursor cursor=null;
        long counter = 0;


        try{
                MongoClient client = new MongoClient("labs-nodejs.elementums.com", 27017);
                DB db = client.getDB("elementum_labs");

                DBCollection receivedTweets = db.getCollection("tweetsReceived");

                /*BasicDBObject query = new BasicDBObject("_id","body");
                cursor = receivedTweets.find(query);*/

                System.out.println("Start---");
                cursor =  receivedTweets.find();

                long totalTweetcount = receivedTweets.getCount();
                //Get Sites

                Set<String> sites = getSites();

                while(cursor.hasNext()){
                    BasicDBObject tweetGot = (BasicDBObject)cursor.next();
                    String _id =    tweetGot.getString("_id");
                    String body =    tweetGot.getString("body");

                    String[] words = body.split(" ");

                    Tweet currentTweet =  new Tweet(tweetGot.getString("_id"),tweetGot.getString("body"));

                    for (String word:words){
                         if(sites.contains(word)){
                               currentTweet.setMatchType("Exact");
                               tweetWithSites.add(currentTweet);
                                counter++;
                            break;

                         }
                    }
                    System.out.println("{serial: "+(counter)+" out of "+totalTweetcount+" ID:"+_id+",body:{"+body+"}}");
                    tweetWithSites.add(currentTweet);
                }

                System.out.println("Tweet Count:"+tweetList.size());
            }catch (Exception e){
            e.printStackTrace();
            }finally{
                cursor.close();
        }
           return tweetWithSites;

    }
}
